package cn.cmcc.online.smsapi.ui.presenter;

import android.view.View;
import android.view.ViewGroup;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.cmcc.online.smsapi.ui.model.CarrierOperatorData;
import cn.cmcc.online.smsapi.ui.model.SmsProgress;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.util.ItemRepertory;
import cn.cmcc.online.smsapi.ui.util.ItemRepertoryBaseImpl;
import cn.cmcc.online.smsapi.ui.widget.ProgressTC;
import cn.cmcc.online.smsapi.ui.widget.TextTC;

import static cn.cmcc.online.smsapi.ui.view.GeneralBaseVistaImpl.WITH_ANIMATION;

/**
 * Created by ZhangYifan on 2017/6/19.
 */

public class CarrierOperatorPresenterImpl extends CarrierOperatorPresenter {
    private static final int MAX_TC_COUNT = 4;

    private CarrierOperatorData mData;

    private ItemRepertory<TextTC> mTextTCRepertory;
    private ItemRepertory<ProgressTC> mProgressTCRepertory;
    private List<TextTC> mShowedTextTCList;
    private List<ProgressTC> mShowedProgressTCList;

    private boolean mIsFirst;

    public CarrierOperatorPresenterImpl() {
        mTextTCRepertory = new TextTCRepertory();
        mProgressTCRepertory = new ProgressTCRepertory();
    }

    @Override
    public void bind(JSONObject data) {
        super.bind(data);

        mData = new CarrierOperatorData(data);
        String endTime = mData.getEndTime();
        if (endTime != null && endTime.length() > 0) {
            vista.setEndTime(endTime);
        } else {
            vista.setEndTimeVisible(View.GONE);
        }

        updateTCList();
    }

    private void updateTCList() {
        recycleTC();

        int itemTotalCount = 0;
        Map<String, String> textList = mData.getTextList();
        if (textList != null && textList.size() > 0) {
            itemTotalCount += textList.size();
            mShowedTextTCList = new ArrayList<>();
            Iterator<Map.Entry<String, String>> iterator = textList.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String title = entry.getKey();
                String content = entry.getValue();
                TextTC textTC = mTextTCRepertory.getOrCreateItem();
                mShowedTextTCList.add(vista.updateTextTC(textTC, title, content));
            }
            vista.setTextList(mShowedTextTCList);
        }

        List<SmsProgress> smsProgressList = mData.getProgressList();
        if (smsProgressList != null && smsProgressList.size() > 0) {
            itemTotalCount += smsProgressList.size();
            mShowedProgressTCList = new ArrayList<>();
            for (SmsProgress smsProgress : smsProgressList) {
                ProgressTC progressTC = mProgressTCRepertory.getOrCreateItem();
                mShowedProgressTCList.add(vista.updateProgressTC(progressTC, smsProgress));

            }
            vista.setProgressBarList(mShowedProgressTCList);
        }

        if (itemTotalCount == 0) {
            vista.setMidListVisible(View.GONE);
        } else {
            vista.setMidListVisible(View.VISIBLE);
        }

        if (itemTotalCount > MAX_TC_COUNT) {
            vista.setExpandButtonVisible(View.VISIBLE);
            vista.setExpandButtonColor(mData.getColor());
            mIsFirst = true;
            showLess();
            mIsFirst = false;
        } else {
            vista.setExpandButtonVisible(View.GONE);
        }
    }

    @Override
    public void showMore() {
        int targetHeight = 0;
        for (TextTC textTC : mShowedTextTCList) {
            textTC.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(textTC.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int height = textTC.getMeasuredHeight();
            targetHeight += height;
        }

        for (ProgressTC progressTC : mShowedProgressTCList) {
            progressTC.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(progressTC.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int height = progressTC.getMeasuredHeight();
            targetHeight += height;
        }

        vista.setTCListHeight(targetHeight + 2, WITH_ANIMATION);
    }

    @Override
    public void showLess() {
        int targetHeight = 0;
        int count = 0;
        for (TextTC textTC : mShowedTextTCList) {
            if (count++ == MAX_TC_COUNT) {
                break;
            }
            textTC.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(textTC.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int height = textTC.getMeasuredHeight();
            targetHeight += height;
        }

        for (ProgressTC progressTC : mShowedProgressTCList) {
            if (count++ == MAX_TC_COUNT) {
                break;
            }
            progressTC.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(progressTC.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int height = progressTC.getMeasuredHeight();
            targetHeight += height;
        }

        if (mIsFirst) {
            vista.setTCListHeight(targetHeight + 2, false);
        } else {
            vista.setTCListHeight(targetHeight + 2, WITH_ANIMATION);
        }
    }

    private void recycleTC() {
        for (ViewGroup viewGroup : vista.recycleAllTC()) {
            if (viewGroup instanceof TextTC) {
                mTextTCRepertory.recycleItem((TextTC) viewGroup);
            } else if (viewGroup instanceof ProgressTC) {
                mProgressTCRepertory.recycleItem((ProgressTC) viewGroup);
            }
        }
    }

    class TextTCRepertory extends ItemRepertoryBaseImpl<TextTC> {

        @Override
        public TextTC createItem() {
            return vista.createTextTC();
        }
    }

    class ProgressTCRepertory extends ItemRepertoryBaseImpl<ProgressTC> {

        @Override
        public ProgressTC createItem() {
            return vista.createProgressTC();
        }
    }
}
